<template>
	<web-view ref="webview" class="y-flex-1" src="/hybrid/html/video/my-short-player.html" @onPostMessage="handleMessage"></web-view>
</template>

<script>
	import { setVolume, getVolume, saveBase64ImageToAlbum } from '@/assets/js/native.js'
	export default {
		inject: ['showVolume', 'download'],
		props: {
			moreShow: {
				type: Boolean,
				default: true
			},
			sliderEnd: {
				type: [Number, String],
				default: ''
			}
		},
		data () {
			return {
				data: [],
				timeList: [],//临时记录列表，用于向webview传输数据
				timeFunction: ''//临时记录函数名，用于向webview传输数据
			}
		},
		methods: {
			handleMessage (e) {
				e.detail.data.forEach(item => {
					if ( item.download >= 0 ) {
						const index = item.download
						if ( !this.data[index].src ) {
							this.toast('未找到视频链接')
							return
						}
						this.download({title: this.data[index].title, url: this.data[index].src, isAdult: this.data[index].isAdult || 0})
					} else if ( item.voice ) {
						this.showVolume()
					} else if ( item.seeklight >= 0 ) {
						uni.setScreenBrightness({
							value: item.seeklight,
							success: () => {
								this.setInitial()
							}
						})
					} else if ( item.seekvolume >= 0 ) {
						setVolume(item.seekvolume)
						this.setInitial()
					} else if ( item.capturesaved ) {
						if ( item.capturesaved.code != 200 ) {
							saveBase64ImageToAlbum(item.capturesaved.data.base64, item.capturesaved.fileName).then(path => {
								this.showToast('保存到相册成功')
							}).catch(() => {
								this.showToast('保存到相册失败')
							})
						}
					} else if ( item.directionchange ) {
						if ( item.directionchange.code != 200 ) {//锁定屏幕方向失败
							var direction = item.directionchange.to//预期屏幕方向
							if ( direction ) plus.screen.lockOrientation(direction)//锁定屏幕方向
							else plus.screen.unlockOrientation()//解除屏幕方向锁定
						}
					} else if ( item.timeListEnd >= 0 ) {
						this.setTimeList(item.timeListEnd)
					} else {
						Object.keys(item).forEach(key => {
							this.$emit(key, item[key])
						})
					}
				})
			},
			play () {
				this.$refs.webview && this.$refs.webview.evalJS(`play()`)
			},
			pause () {
				this.$refs.webview && this.$refs.webview.evalJS(`pause()`)
			},
			slideTo (current) {
				this.$refs.webview && this.$refs.webview.evalJS(`slideTo(${current})`)
			},
			setInitial () {
				return new Promise(resolve => {
					uni.getScreenBrightness({
						success: res => {
							const arg = {volume: getVolume(), light: res.value}
							this.$refs.webview && this.$refs.webview.evalJS(`setInitial('${encodeURIComponent(JSON.stringify(arg))}')`)
							resolve()
						}
					})
				})
			},
			showToast (data) {
				this.$refs.webview && this.$refs.webview.evalJS(`showToast('${encodeURIComponent(JSON.stringify(data))}');`)
			},
			init (data) {
				this.data = data
				this.timeList = data.map(item => Object.assign({}, item, {
					poster: item.cover,
					loop: !this.sliderEnd,
					moreShow: this.moreShow
				}) )
				if ( this.timeList.length > 0 ) {
					this.timeFunction = 'setPulldown'
					this.setTimeList(0)
				} else this.setPulldown('fail')
			},
			insert (data) {
				this.data = this.data.concat(data)
				this.timeList = data.map(item => Object.assign({}, item, {
					poster: item.cover,
					loop: !this.sliderEnd,
					moreShow: this.moreShow
				}) )
				if ( this.timeList.length > 0 ) {
					this.timeFunction = 'setPullup'
					this.setTimeList(0)
				} else this.setPullup('fail')
			},
			setTimeList (index) {
				if ( index == this.timeList.length ) {
					this[this.timeFunction]('success')
				} else {
					const list = this.timeList.slice(index, Math.min(index + 5, this.timeList.length))
					//数据需要转义2次，因为nvue的webview会自动转义一次
					this.$refs.webview && this.$refs.webview.evalJS(`setTimeList('${encodeURIComponent(encodeURIComponent(JSON.stringify(list)))}')`)
				}
			},
			destroy () {
				this.$refs.webview && this.$refs.webview.evalJS(`clearData()`)
			},
			setPulldown (status) {
				this.$refs.webview && this.$refs.webview.evalJS(`setPulldown('${status}')`)
			},
			setPullup (status) {
				this.$refs.webview && this.$refs.webview.evalJS(`setPullup('${status}')`)
			}
		}
	}
</script>

<style>
</style>